package com.zsk.shop.controller.app;

import com.zsk.shop.common.annotion.NoPermission;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.base.page.BaseDateQueryParams;
import com.zsk.shop.common.constant.RedisKeys;
import com.zsk.shop.common.utils.RedisUtil;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.enums.EnumDataDictionaryKey;
import com.zsk.shop.model.params.ShopWithdrawApplyParams;
import com.zsk.shop.model.results.ShopUserAccountListResult;
import com.zsk.shop.model.results.ShopUserAccountResult;
import com.zsk.shop.model.results.WithdrawMethodResult;
import com.zsk.shop.service.UserAccountService;
import com.zsk.shop.service.WithdrawApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 提现操作
 *
 * @author ljh
 * @create 2021/5/17 10:17
 */
@RestController
@Api(tags = "提现相关")
@RequestMapping("/shop/withdrawApply")
public class WithdrawApplyController {

    private static final Logger logger = LoggerFactory.getLogger(WithdrawApplyController.class);

    @Resource
    private WithdrawApplyService withdrawApplyService;

    @Resource
    private UserAccountService userAccountService;

    @ApiOperation("提现申请")
    @PostMapping("/withdraw.do")
    public BaseResponse withdraw(@RequestBody ShopWithdrawApplyParams params) {
        if (ToolUtil.isOneEmpty(params, params.getApplyType(), params.getMoney())) {
            return BaseResponse.error("数据有误");
        }
        String limitWithdrawMoneyStr = RedisUtil.get(RedisKeys.dictionaryKey(EnumDataDictionaryKey.LIMIT_WITHDRAW_MONEY.getKey())).toString();
        BigDecimal limitWithdrawMoney = new BigDecimal("1").setScale(2, BigDecimal.ROUND_HALF_UP);
        if (ToolUtil.isNotEmpty(limitWithdrawMoneyStr)) {
            limitWithdrawMoney = new BigDecimal(limitWithdrawMoneyStr).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        if (params.getMoney().compareTo(limitWithdrawMoney) < 0) {
            return BaseResponse.error("满" + limitWithdrawMoney + "元,方可提现");
        }
        if (params.getMoney().compareTo(new BigDecimal("10000")) > 0) {
            return BaseResponse.error("提现金额不能超过1万元");
        }
        BigDecimal money = params.getMoney();
        return withdrawApplyService.withdraw(params.getApplyType(), params.getBankCardNumber(), money);
    }

    @ApiOperation("余额信息")
    @PostMapping("/getMoneyAccount.do")
    public BaseResponse<ShopUserAccountResult> getMoneyAccount() {
        return userAccountService.getMoneyAccount();
    }

    @ApiOperation("用户余额列表【分页】")
    @PostMapping("/userAccountList.do")
    public BaseResponse<ShopUserAccountListResult> userAccountList(@RequestBody BaseDateQueryParams params) {
        return userAccountService.userAccountList(params);
    }

    @ApiOperation("获取提现方式，列举用户可提现方式")
    @PostMapping("/getWithdrawMethod.do")
    public BaseResponse<WithdrawMethodResult> getWithdrawMethod() {
        return userAccountService.getWithdrawMethod();
    }

    @NoPermission
    @PostMapping("/getMethod.do")
    @ApiOperation("获取提现方式，列举用户支持的可提现方式")
    public BaseResponse getMethod() {
        return BaseResponse.success(userAccountService.getMethod());
    }

}

